let request1 = ()=>{
    return new Promise((resolve,reject)=>{
        setTimeout(()=>{
            resolve(1)
        },1000)
    })
}
let request2 = ()=>{
    return new Promise((resolve,reject)=>{
        setTimeout(()=>{
            resolve(2)
        },3000)
    })
}
let request3 = ()=>{
    return new Promise((resolve,reject)=>{
        setTimeout(()=>{
            resolve(3)
        },5000)
    })
}
let request4 = ()=>{
    return new Promise((resolve,reject)=>{
        setTimeout(()=>{
            resolve(4)
        },2000)
    })
}


function asyncPool(max=3){
    let pool=[]
    let current = 0
    return function(ajax){
        pool.push(ajax)
        current++
        return new Promise((resolve,reject)=>{
            if(current<max){
                resolve(task()).then(()=>{
                    current--
                })
            }else{

            }
            let task = pool.shift()
            resolve(task()).then(()=>{

            })
        })
    }
}
let req = asyncPool(3)
req(request1).then(res=>console.log(res))
req(request2).then(res=>console.log(res))
req(request3).then(res=>console.log(res))

req(request4).then(res=>console.log(res))